package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.analytic.AnalyticsUtil;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.gio.util.ActivityHelper;
import com.a0soft.gphone.aTruffleHog.util.CONFIGURATION;
import com.a0soft.gphone.aTruffleHog.util.CrashHandler;

public class PrefWnd extends PreferenceActivity {
    private static final String KEY_REPORT_BUG = "report_bug";
    private static final String KEY_ABOUT = "about";
    
    final ActivityHelper mActivityHelper = ActivityHelper.createInstance(this);
    
    @Override protected void 
    onCreate(Bundle bdSavedInstanceState)
    {
        super.onCreate(bdSavedInstanceState);
        
        setContentView(R.layout.pref_wnd);
        
        addPreferencesFromResource(R.xml.pref_wnd);
        
        mActivityHelper.setupActionBar(getTitle(), 0);
        
        Preference pref;
        
        pref = findPreference(KEY_REPORT_BUG);
        pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override public boolean 
            onPreferenceClick(Preference preference)
            {
                _ReportBug();
                return true;
            }
        });
        
        pref = findPreference(KEY_ABOUT);
        pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override public boolean 
            onPreferenceClick(Preference preference)
            {
                startActivity(new Intent(PrefWnd.this, AboutWnd.class));
                return true;
            }
        });
        
        AnalyticsUtil.GetInstance(this).TrackPageView(AnalyticsUtil.PAGE_PREF);
    }
    
    @Override protected void 
    onPostCreate(Bundle savedInstanceState) 
    {
        super.onPostCreate(savedInstanceState);
        mActivityHelper.onPostCreate(savedInstanceState);
        mActivityHelper.setupSubActivity();
        
        BaseActivity.SetDefaultBackground(this);
    }
    
    @Override public boolean 
    onCreateOptionsMenu(Menu menu) 
    {
        return mActivityHelper.onCreateOptionsMenu(menu) || super.onCreateOptionsMenu(menu);
    }

    @Override public boolean 
    onOptionsItemSelected(MenuItem item) 
    {
        return mActivityHelper.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }
    
    private void
    _ReportBug()
    {
        new SendBugReportByEmail(this).execute();
    }
    
    /////////////////////////////////////////////////////////////////// 
    protected static class SendBugReportByEmail extends AsyncTask<Object, Void, String> {
        private Context m_ctx;
        
        SendBugReportByEmail(Context context)
        {
            m_ctx = context;
        }
        
        @Override protected void 
        onPreExecute()
        {
            Toast.makeText(m_ctx, R.string.wait, Toast.LENGTH_SHORT).show();
        }

        @Override protected String 
        doInBackground(Object... objs) 
        {
            Context ctx = m_ctx;
            
            StringBuilder sb = new StringBuilder(2048);

            sb.append("Please describe the problem: \n\n");
            
            sb.append(CrashHandler.GetPackageInfo());
            _GetSettings(ctx, sb);
            
            return sb.toString();
        }
        
        private void
        _GetSettings(Context ctx, StringBuilder sb)
        {
            //settings
            sb.append("\nsettings\n=======================\n");
        }
        
        @Override protected void 
        onPostExecute(String strBody) 
        {           
            String strSubject = m_ctx.getString(R.string.app_name) + " bug report";
            
            try {
                Intent it = new Intent(Intent.ACTION_SENDTO, 
                                       Uri.parse("mailto:" + CONFIGURATION.PROJECT_OWNER_EMAIL));
                it.putExtra(Intent.EXTRA_SUBJECT, strSubject);
                it.putExtra(Intent.EXTRA_TEXT, strBody);
                m_ctx.startActivity(it);  
            } catch (ActivityNotFoundException e) {
                new AlertDialog.Builder(m_ctx)
                    .setTitle(R.string.error)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage("No Email app found from your phone!")
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
            }
        }
    }
}
